/**
 * 棋盘链表结构.
 */
//节点类
function Point(data){
    this.data = data;
    this.next = null;
    this.prev = null;
    this.top = null;
    this.down = null;

    // Point.prototype.data = data;
    // Point.prototype.next = next;
    // Point.prototype.prev = prev;
    // Point.prototype.top = top;
    // Point.prototype.down = down;
}

function PointLinkedList(){

    //属性
    this.head = null;   //空链表时head指向null
    this.down = null;

    this.length=0;      //链表长度

    // //向尾部添加一个新的项(尾插法)
    // PointLinkedList.prototype.append=function(data){
    //     //创建一个新节点
    //     var newPoint = new Point(data);
    //
    //     //如果要插入的是第一个链表，让head指向第一个节点
    //     if(this.length === 0){
    //         this.head = newPoint;
    //
    //     } else{
    //         //如果不是第一个节点，则找到链表尾部的最后一个节点，让这个节点指向要插入的节点
    //         var current = this.head;  //此时current指向第一个节点
    //         while(current.next){
    //             current = current.next;
    //         }
    //
    //         newPoint.prev = current;
    //         current.next = newPoint;
    //     }
    //     //插入节点后，链表长度+1
    //     this.length += 1;
    // }

    //添加新行(尾插法)
    PointLinkedList.prototype.appendNewLine = function(data){
        //创建一个新节点
        var newPoint = new Point(data);

        //如果要插入的是第一个链表，让head指向第一个节点
        if(this.length === 0){
            this.head = newPoint;

        } else{
            //找到最后一行，向尾部插入
            var current = this.head;  //此时current指向第一个节点
            while(current.down){
                current = current.down;
            }

            newPoint.top = current;
            current.down = newPoint;
        }


        //插入节点后，链表长度+1
        this.length += 1;
    }

    //插入指定行的尾部(尾插法)
    PointLinkedList.prototype.appendLine = function(data, row){
        if(row < 0) {
            //添加新行
            this.appendNewLine(data);
            return;
        }

        //创建一个新节点
        var newPoint = new Point(data);

        if(this.length === 0){
            this.head = newPoint;

        } else {
            //找到指定行，向尾部插入
            var current = this.head;  //此时current指向第一个节点

            //找到指定行
            var i = 0;
            for (; current.down; i++) {
                if (i === row) {
                    break;
                }
                current = current.down;
            }

            if(i !== row) {
                //添加新行
                this.appendNewLine(data);
                return;
            }

            //找到最后一项
            i = 0;
            while(current.next){
                current = current.next;
                i++;
            }

            current.next = newPoint;
            newPoint.prev = current;

            //连接上边的关系
            if(row > 0) {
                //找到新增节点的上一行对应位置的节点
                var topNode = this.getIndex(i + 1, row - 1);
                if(topNode) {
                    topNode.down = newPoint;
                    newPoint.top = topNode;
                }
            }
        }

        //插入节点后，链表长度+1
        this.length += 1;
    }

    //根据定位信息，获取四象链表中的某一对象
    //x：列  y：行
    PointLinkedList.prototype.getIndex = function(x, y){
        var row = this.head;
        var result = null;
        var current;
        // var current = this.head;


        for (let i = 0; i <= y && row; i++) {
            if(i !== y) {
                //下走一行
                row = row.down;
                continue;
            }

            current = row;
            for (let j = 0; j <= x && current; j++) {
                if(j !== x) {
                    //下走一个
                    current = current.next;
                    continue;
                }

                result = current;
            }
        }

        return result;
    }

    //连接四维链表
    function joinRelation(newPoint) {
        newPoint
    }

    //打印
    PointLinkedList.prototype.console = function(){
        var current;
        var row = this.head;
        var xStr = '';

        do{
            console.log('新行');
            current = row;
            do {
                xStr += current.data.xNum + '-' + current.data.yNum+"       ";
                current = current.next;
            } while (current);
            //下走一行

            console.log(xStr);
            xStr = '';
            row = row.down;
        }while (row);
    }
}